<?php

namespace App\Models\HongBao;

use App\Libraries\Tools\ConstVar;
use App\Models\BaseModel;

class HongBaoTemplate extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'type',
        'title',
        'min_money',
        'max_money',
        'min_consumption',
        'max_consumption',
        'limit_money',
        'min_point',
        'max_point',
        'limit_point',
        'daily_limit',
        'only_once',
        'validity',
        'priority',
        'agent_city_id',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    protected $dateFormat = 'U';

    protected $table = 'hong_bao_template';

    /**
     * 根据类型获取红包模板
     * @param $type
     * @return mixed
     */
    public function getTemplateByType($type){
        return $this->where('type', $type)->where('status', 1)->orderBy('priority', 'desc')->first();
    }

    /**
     * 根据ID获取红包模板
     * @param $id
     * @return mixed
     */
    public function getTemplateById($id){
        return $this->where('id', $id)->where('status', 1)->first();
    }

    public function getShopHBT($shop_id){
        $data = $this->where('shop_id', $shop_id)->where('type', ConstVar::$HONGBAO_TYPE_CASH)
            ->where(function($sql){
                $sql->where('is_stock', 1)->where('stock', '>', 0)->orWhere('is_stock', 0);
            })->where('status', 1)->get();
        return $data;
    }
}
